package com.woniuxy.car.commodity.service.core.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.woniuxy.car.commodity.service.core.constant.FormatLabel;
import com.woniuxy.car.commodity.service.core.dto.CommodityFormatAndParamDto;
import com.woniuxy.car.commodity.service.core.entity.CommodityFormat;
import com.woniuxy.car.commodity.service.core.service.ICommodityFormatService;
import com.woniuxy.common.api.CommonResult;
import com.woniuxy.common.api.enums.ResultCode;
import com.woniuxy.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品规格表 前端控制器
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@Api(tags = "商品规格表")
@RestController
@RequestMapping("/commodity/commodity-format")
public class CommodityFormatController {

    @Autowired(required = false)
    private ICommodityFormatService commodityFormatService;

    @ApiOperation("根据商品id查询规格")
    @GetMapping("/{commodityId}")
    public CommonResult<List<CommodityFormatAndParamDto>> getOneById(@PathVariable("commodityId") Integer commodityId){

        try {
            CommodityFormat commodityFormat = commodityFormatService.findOne(commodityId);
            System.out.println(commodityFormat);

            List<CommodityFormatAndParamDto> commodityFormatDtos = new ArrayList<>();

            if(!(commodityFormat == null)) {
                JSONArray jsonArray = JSONUtil.parseArray(commodityFormat.getFormatData());

                for (Object obj : jsonArray) {

                    JSONObject jsonObject = JSONUtil.parseObj(obj);

                    //得到规格
                    List<String> colorList = (List<String>) jsonObject.get("params");

                    CommodityFormatAndParamDto formatAndParamDto = new CommodityFormatAndParamDto();
                    //得到规格标签
                    String labelName =(String) jsonObject.get("group");

                    if (labelName.equals(FormatLabel.LABEL_COLOR)){
                        formatAndParamDto.setLabel(FormatLabel.LABEL_COLOR);
                    }else if(labelName.equals(FormatLabel.LABEL_SIZE)){
                        formatAndParamDto.setLabel(FormatLabel.LABEL_SIZE);
                    }else if(labelName.equals(FormatLabel.LABEL_TYPE)){
                        formatAndParamDto.setLabel(FormatLabel.LABEL_TYPE);
                    }else{
                        formatAndParamDto.setLabel(null);
                    }
                    formatAndParamDto.setLabelList(colorList);

                    commodityFormatDtos.add(formatAndParamDto);
                }
            }

            return CommonResult.success(commodityFormatDtos);

        }catch (BusinessException e){

            return CommonResult.failure(ResultCode.FAILED.getCode(),e.getMessage());
        }

    }
}
